package com.wan37.logic.email;

import com.wan37.common.uuid.UUIDUtil;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.vo.BackpackVO;
import com.wan37.logic.domain.vo.ItemVO;
import com.wan37.logic.email.domain.vo.AttachmentVO;
import com.wan37.logic.email.domain.vo.EmailVO;
import com.wan37.logic.email.serivce.EmailService;
import com.wan37.logic.email.util.EmailRoutes;
import com.wan37.protobuf.constant.BackpackLabel;
import com.wan37.protobuf.constant.CommonEmail;
import com.wan37.protobuf.constant.ShopLabel;
import com.wan37.protobuf.message.client2server.game.GameEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-28 20:51
 **/
@Component
public class EmailManager {
    @Autowired
    private EmailRoutes emailRoutes;
    @Autowired
    private EmailService emailService;
    @Autowired
    private AsynDBManager asynDBManager;

    public void sendEmail(EmailVO emailVO, List<String> userIdList){
        if (userIdList != null){
            for (String userId : userIdList){
                List<EmailVO> emailVOList = new ArrayList<>();
                emailVOList.add(emailVO);
                emailRoutes.sendEmailResponse(CommonEmail.SAVE, emailVOList, userId);
                asynDBManager.distributeWork(userId, ()->emailService.saveEmailAndAttachment(emailVO));
            }
        }
    }

    /**
     * 发送背包类物品附件
     * @param content 邮件内容
     * @param userId 发送者id
     * @param userIdList 接收者id
     * @param backpackVOList
     */
    public void sendEmail(String content, String userId, List<String> userIdList, List<BackpackVO> backpackVOList){
        String emailId = UUIDUtil.getUUID();
        List<EmailVO> emailVOList = new ArrayList<>();
        for (String userId1 : userIdList){
            EmailVO emailVO = new EmailVO();
            emailVO.setEmailId(emailId);
            emailVO.setContent(content);
            emailVO.setIsRead(1);
            emailVO.setSenderId(userId);
            if (backpackVOList != null){
                List<AttachmentVO> attachmentVOList = new ArrayList<>();
                for (BackpackVO backpackVO : backpackVOList){
                    AttachmentVO attachmentVO = new AttachmentVO();
                    attachmentVO.setBackpackId(backpackVO.getBackpackId());
                    attachmentVO.setType(backpackVO.getLabel());
                    attachmentVO.setStrongLevel(1);
                    if (attachmentVO.getType() == BackpackLabel.WEAPONRY){
                        attachmentVO.setNumber(1);
                    }else if (attachmentVO.getType() == BackpackLabel.ITEM){
                        attachmentVO.setNumber(((ItemVO)(backpackVO)).getCount());
                    }
                    attachmentVO.setEmailId(emailId);
                    attachmentVOList.add(attachmentVO);
                }
                emailVO.setAttachmentVOList(attachmentVOList);
            }
            emailVO.setReceiverId(userId1);
            emailVOList.add(emailVO);
        }
        asynDBManager.distributeWork(userId, ()->emailService.saveEmailAndAttachment(emailVOList));
        List<EmailVO> oneEmailVOList = new ArrayList<>();
        oneEmailVOList.add(emailVOList.get(0));
        emailRoutes.sendEmailBroadcast(CommonEmail.SAVE, oneEmailVOList, userIdList);
    }

    /**
     * 发送元宝或者金币附件的邮件
     * @param content 邮件内容
     * @param userId 发送者Id
     * @param userIdList 接收者id
     * @param type
     * @param count
     */
    public void sendEmail(String content, String userId, List<String> userIdList, int type, long count){
        String emailId = UUIDUtil.getUUID();
        List<EmailVO> emailVOList = new ArrayList<>();
        for (String userId1 : userIdList) {
            EmailVO emailVO = new EmailVO();
            emailVO.setEmailId(emailId);
            emailVO.setContent(content);
            emailVO.setIsRead(1);
            emailVO.setSenderId(userId);
            List<AttachmentVO> attachmentVOList = new ArrayList<>();
            AttachmentVO attachmentVO = new AttachmentVO();
            attachmentVO.setType(type);
            attachmentVO.setBackpackId("1");
            attachmentVO.setNumber(count);
            attachmentVO.setEmailId(emailId);
            attachmentVOList.add(attachmentVO);
            emailVO.setAttachmentVOList(attachmentVOList);
            emailVO.setReceiverId(userId1);
            emailVOList.add(emailVO);
        }
        asynDBManager.distributeWork(userId, ()->emailService.saveEmailAndAttachment(emailVOList));
        List<EmailVO> oneEmailVOList = new ArrayList<>();
        oneEmailVOList.add(emailVOList.get(0));
        emailRoutes.sendEmailBroadcast(CommonEmail.SAVE, oneEmailVOList, userIdList);
    }
}
